package com.meihua.shellapp.activity.wallet;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.MD5Utils;
import com.meihua.shellapp.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

import static com.meihua.shellapp.R.id.add_bank_info_type;

/**
 * 添加银行卡信息
 */
public class AddBankInfoPhoneActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.add_bank_info_type)
    TextView addBankInfoType;
    @Bind(R.id.add_bank_info_phone_number)
    EditText addBankInfoPhoneNumber;
    @Bind(R.id.add_bank_info_address)
    EditText mBankAddress;//开户行地址
    @Bind(R.id.tv_agree_rule)
    TextView tvAgreeRule;
    @Bind(R.id.add_bank_info_next)
    TextView addBankInfoNext;


    private CheckBox checkBoxNew;
    private TextView tv_agree_rule;
    private String realName;
    private String banksCard;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank_info_phone;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        realName = getIntent().getStringExtra("real_name");
        banksCard = getIntent().getStringExtra("banks_card");
        tvTitle.setVisibility(View.VISIBLE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("填写银行卡信息");
        tv_agree_rule = (TextView) findViewById(R.id.tv_agree_rule);
        checkBoxNew = (CheckBox) findViewById(R.id.checkBoxNew);
        tv_agree_rule.setOnClickListener(this);

    }


    private void loadData() {
        Map<String, String> params = new HashMap() {{
            put("mid", MyApplication.getInstance().getMId());
            put("banks_name", addBankInfoType.getText().toString().trim());//银行名称
            put("banks_card", banksCard);//银行账号
            put("open_address", mBankAddress.getText().toString().trim());//开户行
            put("phone", addBankInfoPhoneNumber.getText().toString().trim());//预留手机号
            put("card_name", realName);//开户人
            put("bank_id", addBankInfoType.getTag().toString());
        }};
        NetHelper.post(URL.getUrl().ADDBANK, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(response.body());
                    if (obj.getInt("code") == 1) {
                        Toast.makeText(AddBankInfoPhoneActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
//                        Intent in = new Intent(AddBankInfoPhoneActivity.this, BankcardActivity.class);
//                        startActivity(in);
                        setResult(Constant.ADDBANK_ID, new Intent());
                        finish();
                    } else {
                        Toast.makeText(AddBankInfoPhoneActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, false);
    }


    @OnClick({add_bank_info_type, R.id.add_bank_info_phone_number, R.id.checkBoxNew, R.id.tv_agree_rule, R.id.add_bank_info_next, R.id.iv_top_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case add_bank_info_type:
                //打开银行选择列表
                listDialog();
                break;
            case R.id.iv_top_left:
                finish();
                break;

            case R.id.tv_agree_rule:
                //打开用户协议界面
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("url", "");
                intent.putExtra("title", "填写银行卡信息用户协议");
                startActivity(intent);
                break;
            case R.id.add_bank_info_next:
                //判断是否选中用户协议
                if (!checkBoxNew.isChecked()) {
                    Toast.makeText(this, "请阅读并选中用户协议", Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断是否选择银行卡类型
                if (addBankInfoType.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请选择银行卡类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                String phoneNum = addBankInfoPhoneNumber.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum) || !MD5Utils.isMobileNO(phoneNum)) {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mBankAddress.getText().toString().trim())) {
                    Toast.makeText(this, "开户行地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                loadData();
                break;
        }
    }

    public void listDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(AddBankInfoPhoneActivity.this);
        builder.setTitle("请选择银行");
        final String[] cities = new String[]{"工商银行", "光大银行", "华夏银行", "建设银行", "交通银行", "民生银行", "农业银行", "邮政银行", "招商银行", "中信银行", "兴业银行", "中国银行"};

        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String city = cities[which];
                addBankInfoType.setText(city);
                addBankInfoType.setTag(which + 1);
            }
        });
        builder.show();
    }

}
